<template>
  <div id="app">
    <div class="background">
      <img :src="imgSrc" width="100%" height="100%" alt="" />
    </div>
    <div class="front" >
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="50px" class="demo-ruleForm">
      <h3 class="login_title">GWIR管理员登录</h3>
      <el-form-item label="账号" prop="emailOrNickname">
        <el-input type="text" v-model="ruleForm.account" autocomplete="off" placeholder="请输入账号"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="checkPass" >
        <el-input type="password" v-model="ruleForm.password" autocomplete="off" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="ToLogin">用户登录</el-button>
        <el-button type="primary" @click="subAjax">登录</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button><br>
      </el-form-item>
    </el-form>
      </div>
  </div>
</template>

<script>

import axios from 'axios'

export default {
  name: 'AdminLogin',
  data () {
    var checkAccount = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入账号'))
      } else {
        callback()
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    return {
      imgSrc: require('../assets/Login.jpg'),
      ruleForm: {
        account: '',
        password: ''
      },
      rules: {
        account: [
          { validator: checkAccount, trigger: 'blur' }
        ],
        password: [
          { validator: validatePass, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    ToLogin () {
      this.$router.push(({path: '/login'}))
    },
    subAjax: function () {
      axios.post(`admin/login`, {
        account: this.ruleForm.account,
        password: this.ruleForm.password
      }).then(res => {
        // let isLogin = res.status // 这里接收状态码
        if (res.data.status === 100) { // 100可换
          if (res.data.data.flag === 200) {
            localStorage.setItem('token', res.data.data.msg)// 存到localStorage里面
            return this.$router.push({
              path: '/AdminIndex'
            })
          } else {
            alert(res.data.data.msg)
          }
        }
        console.log(res)
      }).catch(err => {
        console.log(err)
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>
.demo-ruleForm {
  border-radius: 15px;
  background-clip: padding-box;
  margin-left: 750px ;
  margin-right: 750px ;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

.login_title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
.background{
  width:100%;
  height:100%;  /**宽高100%是为了图片铺满屏幕 */
  z-index:-1;
  position: absolute;
}

.front{
  z-index:1;
  /*position: absolute;*/
  margin-top: 0px ;
  text-align: center;
}
</style>
